package com.sls.web.manager.action.recharge;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.PhoneType;
import com.sls.web.manager.recharge.pojo.ProductVO;
import com.sls.web.manager.recharge.pojo.ProductView;
import com.sls.web.manager.recharge.querybean.AreaQuery;
import com.sls.web.manager.recharge.querybean.OperationQuery;
import com.sls.web.manager.recharge.querybean.ProductQuery;
import com.sls.web.manager.recharge.service.AreaService;
import com.sls.web.manager.recharge.service.FaceValueService;
import com.sls.web.manager.recharge.service.OperationService;
import com.sls.web.manager.recharge.service.PhoneTypeService;
import com.sls.web.manager.recharge.service.ProductService;

public class ProductAction extends SuperAction {
	private static final long serialVersionUID = -5400615207972560221L;
	@Resource(name = "productService")
	private ProductService productService;
	@Resource(name = "operationService")
	private OperationService operationService;
	@Resource(name = "faceValueService")
	private FaceValueService faceValueService;
	@Resource(name = "areaService")
	private AreaService areaService;
	@Resource(name = "phoneTypeService")
	private PhoneTypeService phoneTypeService;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private ProductQuery productQuery;
	private OperationQuery operationQuery;
	private AreaQuery areaQuery;
	private ProductVO product;
	private String id;
	private List<Integer> faceValue;
	private List<PhoneType> phoneTypeList;
	private List<ProductView> productViewList;
	@JSON(serialize = false)
	public List<ProductView> getProductViewList() {
		return productViewList;
	}

	public void setProductViewList(List<ProductView> productViewList) {
		this.productViewList = productViewList;
	}

	@JSON(serialize = false)
	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}
	@JSON(serialize = false)
	public ProductQuery getProductQuery() {
		return productQuery;
	}

	public void setProductQuery(ProductQuery productQuery) {
		this.productQuery = productQuery;
	}
	@JSON(serialize = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	@JSON(serialize = false)
	public List<Integer> getFaceValue() {
		return faceValue;
	}

	public void setFaceValue(List<Integer> faceValue) {
		this.faceValue = faceValue;
	}
	@JSON(serialize = false)
	public OperationQuery getOperationQuery() {
		return operationQuery;
	}

	public void setOperationQuery(OperationQuery operationQuery) {
		this.operationQuery = operationQuery;
	}
	@JSON(serialize = false)
	public ProductVO getProduct() {
		return product;
	}

	public void setProduct(ProductVO product) {
		this.product = product;
	}
	@JSON(serialize = false)
	public AreaQuery getAreaQuery() {
		return areaQuery;
	}

	public void setAreaQuery(AreaQuery areaQuery) {
		this.areaQuery = areaQuery;
	}
	@JSON(serialize = false)
	public List<PhoneType> getPhoneTypeList() {
		return phoneTypeList;
	}

	public void setPhoneTypeList(List<PhoneType> phoneTypeList) {
		this.phoneTypeList = phoneTypeList;
	}

	// 添加
	public String add() throws Exception {
		product = new ProductVO();
		phoneTypeList= phoneTypeService.listEnable(this.getCurrentOperator());
		faceValue=faceValueService.queryFaceValueList(this.getCurrentOperator());
		product.setOperationList(operationService.findList(
				this.getCurrentOperator()));
		return "edit";
	}

	// 添加商品
	public String addProduct() throws Exception {
		try {
			product.setFaceValueList(this.getFaceValue());
			
			productService.addProduct(product, this.getCurrentOperator());
			redirectionUrl = "product!list.htm";
			return "success";
		} catch (ManagerException e) {
			product = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	// 列表
	public String list() {
		if (productQuery == null) {
			productQuery = new ProductQuery();
			productQuery.setPageSize(Constants.INIT_PAGESIZE);
			productQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = productService.queryProduct(productQuery,
					getCurrentOperator());
			if (page != null && page.getList() != null
					&& !page.getList().isEmpty()) {
				for (Iterator<?> it = page.getList().iterator(); it.hasNext();) {
					ProductVO productVO = (ProductVO) it
							.next();
					areaQuery = new AreaQuery();
					areaQuery.setProductId(productVO.getId());
					productVO.setAreaList(areaService.queryList(areaQuery,
							this.getCurrentOperator()));
				}

			}
		} catch (Exception e) {
			return ERROR;
		}

		return "list";
	}

	// 删除
	public String delete() throws Exception { 
		try{
			productService.delete(id, getCurrentOperator());
			redirectionUrl = "product!list.htm";
			return "success";
		}catch (ManagerException e){
			addActionError(e.getMessage());
			return this.list();
		}
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			product = productService.queryProductById(id,
					this.getCurrentOperator());
			faceValue=faceValueService.queryFaceValueList(this.getCurrentOperator());
			product.setOperationList(operationService.findList(
					this.getCurrentOperator()));
			phoneTypeList= phoneTypeService.listEnable(this.getCurrentOperator());
		}
		return "edit";
	}

	// 编辑商品
	public String editProduct() throws Exception {
		product.setFaceValueList(this.getFaceValue());
		boolean result = productService.updateProduct(product,
				this.getCurrentOperator());
		if (!result) {
			addActionError("修改失败!");
			return this.edit();
		} else {
			redirectionUrl = "product!list.htm";
			return "success";
		}
	}
	// 校验商品名称是否已经存在
	public String checkName() throws IOException {
		message = String.valueOf( productService.checkName(product,
				this.getCurrentOperator()));
		return "ajax";
	}
	//查看商品详情
	public String view() throws Exception {
		productViewList=productService.queryProductViewById(id,this.getCurrentOperator());
		return "view"; 
	}
}